package com.yashi.appController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.yashi.entity.TbUser;
import com.yashi.entity.TbUserChat;
import com.yashi.filter.UserFilter;
import com.yashi.service.GroupService;
import com.yashi.service.UserChatService;
import com.yashi.service.UserService;

@RestController
@RequestMapping("/userChat")
public class UserChatController {
	@Autowired
	private UserChatService userChatService;
	@Autowired
	private GroupService groupService;
	@Autowired
	private UserService userService;

	/**
	 * 查询用户聊天记录
	 * 
	 * @param session
	 * @return
	 */
	@RequestMapping("/selectUserChat")
	public Map<String, Object> selectUserChat(Integer startPage, String receiveId, HttpSession session) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			TbUserChat userChat = new TbUserChat();
			userChat.setReceiveId(receiveId);
			PageInfo<TbUserChat> pageInfo = userChatService.selectUserChat(userChat, startPage, session);
			if (pageInfo.getList().size() == 0) {
				map.put("status", "0");
				map.put("message", "无聊天记录");
				map.put("data", pageInfo.getList());
				return map;
			}
			map.put("status", "1");
			map.put("message", "用户聊天记录查询成功");
			map.put("data", pageInfo.getList());
			return map;
		} catch (Exception e) {
			map.put("status", "0");
			map.put("message", "用户聊天记录查询失败");
			return map;
		}
	}

	/**
	 * 查询消息列表
	 * 
	 * @param session
	 * @return
	 */
	@RequestMapping("/findUserChatList")
	public Map<String, Object> findUserChatList(HttpSession session) {
		Map<String, Object> map = new HashMap<String, Object>();
		TbUserChat userChat = new TbUserChat();
		List<TbUser> haveLastUserChatUserList = new ArrayList<>();
		try {
			List<TbUser> userList = userChatService.findUserChatList(session);
			for (TbUser tbUser : userList) {
				userChat.setReceiveId(tbUser.getId());
				List<TbUserChat> lastUserChat = userChatService.selectLastUserChat(userChat, session);
				tbUser.setUserChatList(lastUserChat);
				haveLastUserChatUserList.add(tbUser);
			}
			map.put("data", haveLastUserChatUserList);
			map.put("status", "1");
			map.put("message", "用户列表");
			return map;
		} catch (Exception e) {
			map.put("status", "0");
			map.put("message", "用户列表查询失败");
			return map;
		}
	}

	/**
	 * 查询群组聊天记录
	 * 
	 * @param session
	 * @return
	 */
	@RequestMapping("/selectGroupChat")
	public Map<String, Object> finGroupChatList(Integer startPage, String receiveId, HttpSession session) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			TbUserChat userChat = new TbUserChat();
			userChat.setReceiveId(receiveId);
			PageInfo<TbUserChat> pageInfo = userChatService.selectGroupChat(userChat, startPage);
			if (pageInfo.getList().size() == 0) {
				map.put("status", "0");
				map.put("message", "已无聊天记录");
				map.put("data", pageInfo.getList());
				return map;
			}
			map.put("status", "1");
			map.put("message", "群组聊天记录查询成功");
			map.put("data", pageInfo.getList());
			return map;
		} catch (Exception e) {
			map.put("status", "0");
			map.put("message", "群组聊天记录查询失败");
			return map;
		}
	}

	/**
	 * 添加用户聊天记录
	 * 
	 * @param session
	 * @return 参数：receiveId,content,type
	 */
	@RequestMapping("/saveUserChat")
	public Map<String, Object> saveUserChat(TbUserChat userChat, HttpSession session) {
		Map<String, Object> map = new HashMap<>();
		TbUser myUser = (TbUser) session.getAttribute("user");
		// 将当前用户信息加入聊天记录中
		UserFilter userFilter = new UserFilter();
		userFilter.setId(myUser.getId());
		userFilter.setMail(myUser.getMail());
		userFilter.setPhone(myUser.getPhone());
		userFilter.setPhoto(myUser.getPhoto());
		userFilter.setPosition(myUser.getPosition());
		userFilter.setUsername(myUser.getUsername());
		userChat.setUser(userFilter);
		userChat.setSendId(myUser.getId());
		userChat.setFiletype("2");
		try {
			if (userChat.getType().equals("0")) { // 用户之间发送
				WebSocket.sendInfo(userChat, userChat.getReceiveId());
			} else if (userChat.getType().equals("1")) { // 群组发送消息
				List<TbUser> userList = groupService.selectUserbyGroupId(userChat.getReceiveId());
				for (TbUser user : userList) {// 遍历群组中的用户分别发送
					// 排除当前用户
					if (!user.getId().equals(myUser.getId())) {
						WebSocket.sendInfo(userChat, user.getId());
					}
				}
			}

		} catch (Exception e) {
			map.put("status", "0");
			map.put("message", "用户聊天记录添加失败");
			return map;
		}
		TbUserChat saveUserChat = userChatService.saveUserChat(userChat, session);
		map.put("status", "1");
		map.put("message", "用户聊天记录添加成功");
		map.put("data", saveUserChat);
		return map;
	}

	/**
	 * 转发用户聊天记录
	 * 
	 * @param session
	 * @return 参数：receiveId,content,type,filetype,fileName,fileUrl
	 */
	@RequestMapping("/transmitUserChat")
	public Map<String, Object> transmitUserChat(TbUserChat userChat, HttpSession session) {
		Map<String, Object> map = new HashMap<>();
		TbUser myUser = (TbUser) session.getAttribute("user");
		// 将当前用户信息加入聊天记录中
		UserFilter userFilter = new UserFilter();
		userFilter.setId(myUser.getId());
		userFilter.setMail(myUser.getMail());
		userFilter.setPhone(myUser.getPhone());
		userFilter.setPhoto(myUser.getPhoto());
		userFilter.setPosition(myUser.getPosition());
		userFilter.setUsername(myUser.getUsername());
		userChat.setUser(userFilter);
		userChat.setSendId(myUser.getId());
		try {
			if (userChat.getType().equals("0")) { // 用户之间发送
				WebSocket.sendInfo(userChat, userChat.getReceiveId());
			} else if (userChat.getType().equals("1")) { // 群组发送消息
				List<TbUser> userList = groupService.selectUserbyGroupId(userChat.getReceiveId());
				for (TbUser user : userList) {// 遍历群组中的用户分别发送
					// 排除当前用户
					if (!user.getId().equals(myUser.getId())) {
						WebSocket.sendInfo(userChat, user.getId());
					}
				}
			}

		} catch (Exception e) {
			map.put("status", "0");
			map.put("message", "用户聊天记录添加失败");
			return map;
		}
		TbUserChat saveUserChat = userChatService.saveUserChat(userChat, session);
		map.put("status", "1");
		map.put("message", "用户聊天记录添加成功");
		map.put("data", saveUserChat);
		return map;
	}

	/**
	 * 实现多文件上传
	 * 
	 * 需要参数: 上传文件的变量名为：file 接收者id:receiveId 发送者id：sendId 发送者姓名:sendName
	 * 个人发送文件还是群组发送文件:type 文件类型：filetype
	 */
	@RequestMapping(value = "/multifileUpload")
	public Map<String, Object> multifileUpload(HttpServletRequest request, TbUserChat userChat) {
		Map<String, Object> map = new HashMap<String, Object>();
		UserFilter userFilter = new UserFilter();
		List<TbUserChat> userCharList = new ArrayList<>();
		try {
			// 进行文件上传
			List<TbUserChat> fileUrlList = userChatService.multifileUpload(request, userChat);
			// 遍历文件上传后返回的消息结果
			for (TbUserChat tbUserChat : fileUrlList) {
				// 根据发送者id拿到发送者信息
				TbUser selectUserById = userService.selectUserById(tbUserChat.getSendId());
				// 将信息放入userFilter中
				userFilter.setId(selectUserById.getId());
				userFilter.setMail(selectUserById.getMail());
				userFilter.setPhone(selectUserById.getPhone());
				userFilter.setPhoto(selectUserById.getPhoto());
				userFilter.setPosition(selectUserById.getPosition());
				userFilter.setUsername(selectUserById.getUsername());
				// 将userFilter放入tbUserChat也就是消息中
				tbUserChat.setUser(userFilter);
				userCharList.add(tbUserChat);
				if (tbUserChat.getType().equals("0")) { // 用户之间发送
					WebSocket.sendInfo(tbUserChat, tbUserChat.getReceiveId());
				} else if (userChat.getType().equals("1")) { // 群组发送消息
					List<TbUser> userList = groupService.selectUserbyGroupId(tbUserChat.getReceiveId());
					for (TbUser user : userList) {// 遍历群组中的用户分别发送
						// 排除当前用户
						if (!user.getId().equals(userChat.getSendId())) {
							WebSocket.sendInfo(userChat, user.getId());
						}
					}
				}
				;
			}
			map.put("status", "1");
			map.put("message", "文件上传成功");
			map.put("data", userCharList);
			return map;
		} catch (Exception e) {
			map.put("status", "0");
			map.put("message", "文件上传失败");
			return map;
		}
	}

	/**
	 * 查看群文件
	 * 
	 * @param receiveId
	 * @return
	 */
	@RequestMapping(value = "/getFiles")
	public Map<String, Object> getFiles(Integer startPage, String receiveId) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			PageInfo<TbUserChat> pageInfo = userChatService.selectFiles(startPage, receiveId);
			map.put("status", "1");
			map.put("message", "群文件查询成功");
			map.put("data", pageInfo.getList());
			return map;
		} catch (Exception e) {
			map.put("status", "0");
			map.put("message", "群文件查询失败");
			return map;
		}
	}

	/**
	 * 查看个人文件
	 * 
	 * @param receiveId
	 * @return
	 */
	@RequestMapping(value = "/getUserFiles")
	public Map<String, Object> getUserFiles(Integer startPage, String receiveId, HttpSession session) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			TbUserChat userChat = new TbUserChat();
			userChat.setReceiveId(receiveId);
			PageInfo<TbUserChat> pageInfo = userChatService.selectUserFiles(userChat, startPage, session);
			map.put("status", "1");
			map.put("message", "个人文件查询成功");
			map.put("data", pageInfo.getList());
			return map;
		} catch (Exception e) {
			map.put("status", "0");
			map.put("message", "个人文件查询失败");
			return map;
		}

	}

	/**
	 * 添加群组聊天记录
	 * 
	 * @param session
	 * @return
	 */
//	@RequestMapping("/saveGroupChat")
//	public Integer saveGroupChat( TbUserChat userChat, HttpSession session){
//		Integer saveGroupChat = userChatService.saveGroupChat(userChat, session);
//		return saveGroupChat;
//	}
}
